package IO.filesystem;

import java.io.File;

public class FileListing {
	
	int level = 0;
	int fileCount = 0;
	
	/* Recursive function*/
	public void listAllDir(String path){
		File file = new File(path);
		if (!file.exists()) return;
		
		if (file.isFile()) {
			fileCount ++;
		}
		
		System.out.println(file.getName());
		
		level ++;
		if (file.isDirectory()) {
			for (String subPath : file.list()) {
				for (int i=0; i<level; i++)
					System.out.print("  ");
				File subFile = new File(file.getAbsolutePath() + "/" + subPath);
				if (subFile.isDirectory()) listAllDir(subFile.getAbsolutePath());
				else if (subFile.isFile()) {
					System.out.println(subFile.getName());
					fileCount ++;
				}
			}
		} 
		
		level --;
	}
	
	public static void main(String[] args) {
		String path = "src";
		
		FileListing fl = new FileListing();
		fl.listAllDir(path);
		System.out.println("Number of file: " + fl.fileCount);
	}
}
